﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General

Namespace DoAnQuanLyBanHang.DTO
    Public Class NhanVienData
        Private m_NhanVienData As New DataService()

        Public Function LayNhanVien() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Nhan_Vien")
            m_NhanVienData.Load(cmd)
            Return m_NhanVienData
        End Function
        Public Function LayDSNhanVien() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Nhan_Vien NV INNER JOIN Quay_Giao_Dich QGD ON NV.MaQuayGiaoDich = QGD.MaQuayGiaoDich")
            m_NhanVienData.Load(cmd)
            Return m_NhanVienData
        End Function

        Public Function ThemDongMoi() As DataRow
            Return m_NhanVienData.NewRow()
        End Function

        Public Sub ThemNhanVien(m_Row As DataRow)
            m_NhanVienData.Rows.Add(m_Row)
        End Sub

        Public Function LuuNhanVien() As Boolean
            Return m_NhanVienData.ExecuteNoneQuery() > 0
        End Function

        Public Sub LuuNhanVien(manv As [String], hoten As [String], ngaysinh As DateTime, diachi As [String], dienthoai As [String], maquaygd As [String])
            Dim cmd As New SqlCommand("INSERT INTO Nhan_Vien VALUES(@MaNV, @HoTenNV, @NgaySinh, @DiaChi, @DienThoai,  @MaQuayGD)")
            cmd.Parameters.Add("MaNV", SqlDbType.VarChar).Value = manv
            cmd.Parameters.Add("HoTenNV", SqlDbType.NVarChar).Value = hoten
            cmd.Parameters.Add("NgaySinh", SqlDbType.DateTime).Value = ngaysinh
            cmd.Parameters.Add("DiaChi", SqlDbType.NVarChar).Value = diachi
            cmd.Parameters.Add("DienThoai", SqlDbType.VarChar).Value = dienthoai
            cmd.Parameters.Add("MaQuayGD", SqlDbType.VarChar).Value = maquaygd

            m_NhanVienData.Load(cmd)
        End Sub

        Public Sub SuaNhanVien(manv As [String], hoten As [String], ngaysinh As DateTime, diachi As [String], dienthoai As [String], maquaygd As [String])
            Dim cmd As New SqlCommand("UPDATE Nhan_Vien SET HoTen = @HoTenNV, NgaySinh = @NgaySinh, DiaChi = @DiaChi, DienThoai = @DienThoai,  MaQuayGiaoDich = @MaQuayGD WHERE MaNV = @MaNV")
            cmd.Parameters.Add("MaNV", SqlDbType.VarChar).Value = manv
            cmd.Parameters.Add("HoTenNV", SqlDbType.NVarChar).Value = hoten
            cmd.Parameters.Add("NgaySinh", SqlDbType.DateTime).Value = ngaysinh
            cmd.Parameters.Add("DiaChi", SqlDbType.NVarChar).Value = diachi
            cmd.Parameters.Add("DienThoai", SqlDbType.VarChar).Value = dienthoai
            cmd.Parameters.Add("MaQuayGD", SqlDbType.VarChar).Value = maquaygd

            m_NhanVienData.Load(cmd)
        End Sub

        Public Sub XoaNhanVien(manv As [String])
            Dim cmd As New SqlCommand("DELETE FROM Nhan_Vien WHERE MaNV = @MaNV")
            cmd.Parameters.Add("MaNV", SqlDbType.VarChar).Value = manv
            m_NhanVienData.Load(cmd)
        End Sub

#Region "tim kiem"
        Public Function TimKiemTheoMa(id As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Nhan_Vien WHERE MaNV LIKE '%' + @id + '%'")
            cmd.Parameters.Add("id", SqlDbType.VarChar).Value = id

            m_NhanVienData.Load(cmd)
            Return m_NhanVienData
        End Function

        Public Function TimKiemTheoTen(ten As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Nhan_Vien WHERE HoTenNV LIKE '%' + @ten + '%'")
            cmd.Parameters.Add("ten", SqlDbType.NVarChar).Value = ten

            m_NhanVienData.Load(cmd)
            Return m_NhanVienData
        End Function

#End Region
    End Class
End Namespace
